{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>discount%</th>\n",
       "      <th>Food%</th>\n",
       "      <th>Fresh%</th>\n",
       "      <th>Drinks%</th>\n",
       "      <th>Home%</th>\n",
       "      <th>Beauty%</th>\n",
       "      <th>Health%</th>\n",
       "      <th>Baby%</th>\n",
       "      <th>Pets%</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>customer</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14.11</td>\n",
       "      <td>14.07</td>\n",
       "      <td>73.20</td>\n",
       "      <td>4.36</td>\n",
       "      <td>6.20</td>\n",
       "      <td>2.18</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17.85</td>\n",
       "      <td>17.76</td>\n",
       "      <td>52.91</td>\n",
       "      <td>17.76</td>\n",
       "      <td>3.21</td>\n",
       "      <td>2.31</td>\n",
       "      <td>4.35</td>\n",
       "      <td>1.69</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.97</td>\n",
       "      <td>24.10</td>\n",
       "      <td>22.29</td>\n",
       "      <td>38.69</td>\n",
       "      <td>14.92</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.10</td>\n",
       "      <td>23.83</td>\n",
       "      <td>51.28</td>\n",
       "      <td>8.22</td>\n",
       "      <td>14.77</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.90</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4.37</td>\n",
       "      <td>24.84</td>\n",
       "      <td>51.08</td>\n",
       "      <td>10.29</td>\n",
       "      <td>13.04</td>\n",
       "      <td>0.68</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.07</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10234</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10235</th>\n",
       "      <td>0.00</td>\n",
       "      <td>5.80</td>\n",
       "      <td>0.00</td>\n",
       "      <td>51.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>42.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10236</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10237</th>\n",
       "      <td>0.00</td>\n",
       "      <td>4.62</td>\n",
       "      <td>0.00</td>\n",
       "      <td>88.74</td>\n",
       "      <td>6.64</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10238</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10239 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          discount%  Food%  Fresh%  Drinks%  Home%  Beauty%  Health%   Baby%  \\\n",
       "customer                                                                       \n",
       "0             14.11  14.07   73.20     4.36   6.20     2.18     0.00    0.00   \n",
       "1             17.85  17.76   52.91    17.76   3.21     2.31     4.35    1.69   \n",
       "2              2.97  24.10   22.29    38.69  14.92     0.00     0.00    0.00   \n",
       "3              4.10  23.83   51.28     8.22  14.77     0.00     0.00    1.90   \n",
       "4              4.37  24.84   51.08    10.29  13.04     0.68     0.00    0.07   \n",
       "...             ...    ...     ...      ...    ...      ...      ...     ...   \n",
       "10234          0.00   0.00    0.00     0.00   0.00     0.00     0.00  100.00   \n",
       "10235          0.00   5.80    0.00    51.30   0.00     0.00     0.00    0.00   \n",
       "10236          0.00   0.00    0.00     0.00   0.00   100.00     0.00    0.00   \n",
       "10237          0.00   4.62    0.00    88.74   6.64     0.00     0.00    0.00   \n",
       "10238          0.00   0.00    0.00     0.00   0.00     0.00     0.00  100.00   \n",
       "\n",
       "          Pets%  \n",
       "customer         \n",
       "0           0.0  \n",
       "1           0.0  \n",
       "2           0.0  \n",
       "3           0.0  \n",
       "4           0.0  \n",
       "...         ...  \n",
       "10234       0.0  \n",
       "10235      42.9  \n",
       "10236       0.0  \n",
       "10237       0.0  \n",
       "10238       0.0  \n",
       "\n",
       "[10239 rows x 9 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "# 数据预处理\n",
    "df = pd.read_csv(\"../data/order.csv\")\n",
    "df.drop(['order','total_items','weekday','hour'],axis=1,inplace=True)\n",
    "# 计算同一客户的下单情况平均值\n",
    "df_dispose = df.groupby(by='customer').mean().round(2)\n",
    "df_dispose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 利用Linkage()方法进行分层聚类\n",
    "# import numpy as np\n",
    "# import pandas as pd\n",
    "# import matplotlib.pyplot as plt\n",
    "# from scipy.cluster.hierarchy import dendrogram, linkage\n",
    "# # 能正常显示中文\n",
    "# plt.rcParams['font.sans-serif'] = ['SimHei']\t\t# 用来正常显示中文标签\n",
    "# plt.rcParams['axes.unicode_minus'] = False\t\t\t# 用来正常显示负号\n",
    "\n",
    "\n",
    "# # 生成距离矩阵\n",
    "# dist_matrix = linkage(df_dispose, method='average', metric='euclidean')\n",
    "\n",
    "# # 绘制树状图\n",
    "# plt.figure(figsize=(20, 10))\n",
    "# plt.title('层次聚类树状图')\n",
    "# plt.xlabel('样本指数')\n",
    "# plt.ylabel('距离')\n",
    "# dendrogram(dist_matrix, leaf_rotation=90., leaf_font_size=8.)\n",
    "# # 将图保存到tmp文件中\n",
    "# plt.savefig(\"../tmp/层次聚类树状图.png\")\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  AgglomerativeClustering实现分层聚类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.cluster.hierarchy import dendrogram, linkage\n",
    "from sklearn.cluster import AgglomerativeClustering\n",
    "from sklearn.metrics import silhouette_score\n",
    "\n",
    "# 计算轮廓系数\n",
    "def get_silhouette_score(cluster_data, n_clusters):\n",
    "    # 构建训练\n",
    "    # 分别使用ward，complete，average三种不同的合并策略测试他的效果\n",
    "    clustering = AgglomerativeClustering(n_clusters=n_clusters)\n",
    "    labels = clustering.fit_predict(cluster_data)\n",
    "    # 返回轮廓系数\n",
    "    return silhouette_score(cluster_data, labels)\n",
    "\n",
    "# 寻找最优的n_clusters\n",
    "def find_best_n_clusters(cluster_data):\n",
    "    scores = []\n",
    "    # 寻找2到14之间寻找最优取值\n",
    "    for n_clusters in range(2, 15):\n",
    "        # 调用轮廓系数计算\n",
    "        score = get_silhouette_score(cluster_data, n_clusters)\n",
    "        scores.append(score)\n",
    "        print(\"n_clusters为：\",n_clusters,\"时，他的轮廓系数为：\",score)\n",
    "    # 利用numpy库中的argmax寻找最优值的位置\n",
    "    best_n_clusters = np.argmax(scores) + 2\n",
    "    return best_n_clusters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n_clusters为： 2 时，他的轮廓系数为： 0.3825832568786637\n",
      "n_clusters为： 3 时，他的轮廓系数为： 0.4170974831458286\n",
      "n_clusters为： 4 时，他的轮廓系数为： 0.4498128321963046\n",
      "n_clusters为： 5 时，他的轮廓系数为： 0.26969034021653215\n",
      "n_clusters为： 6 时，他的轮廓系数为： 0.30297419693530264\n",
      "n_clusters为： 7 时，他的轮廓系数为： 0.32114344671177714\n",
      "n_clusters为： 8 时，他的轮廓系数为： 0.32696295854467894\n",
      "n_clusters为： 9 时，他的轮廓系数为： 0.3256515707902876\n",
      "n_clusters为： 10 时，他的轮廓系数为： 0.31344527307210196\n",
      "n_clusters为： 11 时，他的轮廓系数为： 0.27157630712264674\n",
      "n_clusters为： 12 时，他的轮廓系数为： 0.2713866927017632\n",
      "n_clusters为： 13 时，他的轮廓系数为： 0.2687120338281815\n",
      "n_clusters为： 14 时，他的轮廓系数为： 0.27297505911966363\n",
      "最优best_n_clusters为： 4\n",
      "最优轮廓系数为： 0.4498128321963046\n"
     ]
    }
   ],
   "source": [
    "# 层次聚类模型,主要是看选取的簇是关键\n",
    "# 利用通过轮廓系数来选择最优的n_clusters\n",
    "# 具体来说，轮廓系数是对于每个点i，它的簇内距离a(i)和该点到其最近邻簇的平均距离b(i)之差，除以这两者中的较大值。总的轮廓系数则是各点轮廓系数的均值。\n",
    "# 要选择最优的n_clusters，需要在不同的n_clusters取值下，计算轮廓系数的平均值，并找到使平均轮廓系数最大的n_clusters数目。\n",
    "best_n_clusters = find_best_n_clusters(df_dispose)\n",
    "score = get_silhouette_score(df_dispose, best_n_clusters)\n",
    "print(\"最优best_n_clusters为：\",best_n_clusters)\n",
    "print(\"最优轮廓系数为：\",score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用分层聚类模型分析每一类人的消费趋向"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练模型时不对同一客户的下单情况取平均平均值,增大训练量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>discount%</th>\n",
       "      <th>Food%</th>\n",
       "      <th>Fresh%</th>\n",
       "      <th>Drinks%</th>\n",
       "      <th>Home%</th>\n",
       "      <th>Beauty%</th>\n",
       "      <th>Health%</th>\n",
       "      <th>Baby%</th>\n",
       "      <th>Pets%</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>23.03</td>\n",
       "      <td>9.46</td>\n",
       "      <td>87.06</td>\n",
       "      <td>3.48</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.22</td>\n",
       "      <td>15.87</td>\n",
       "      <td>75.80</td>\n",
       "      <td>6.22</td>\n",
       "      <td>2.12</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.08</td>\n",
       "      <td>16.88</td>\n",
       "      <td>56.75</td>\n",
       "      <td>3.37</td>\n",
       "      <td>16.48</td>\n",
       "      <td>6.53</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.51</td>\n",
       "      <td>28.81</td>\n",
       "      <td>35.99</td>\n",
       "      <td>11.78</td>\n",
       "      <td>4.62</td>\n",
       "      <td>2.87</td>\n",
       "      <td>15.92</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>18.31</td>\n",
       "      <td>24.13</td>\n",
       "      <td>60.38</td>\n",
       "      <td>7.78</td>\n",
       "      <td>7.72</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29995</th>\n",
       "      <td>0.00</td>\n",
       "      <td>5.80</td>\n",
       "      <td>0.00</td>\n",
       "      <td>51.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>42.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29996</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29997</th>\n",
       "      <td>0.00</td>\n",
       "      <td>9.25</td>\n",
       "      <td>0.00</td>\n",
       "      <td>77.48</td>\n",
       "      <td>13.27</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29998</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29999</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30000 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       discount%  Food%  Fresh%  Drinks%  Home%  Beauty%  Health%  Baby%  \\\n",
       "0          23.03   9.46   87.06     3.48   0.00     0.00     0.00    0.0   \n",
       "1           1.22  15.87   75.80     6.22   2.12     0.00     0.00    0.0   \n",
       "2          18.08  16.88   56.75     3.37  16.48     6.53     0.00    0.0   \n",
       "3          16.51  28.81   35.99    11.78   4.62     2.87    15.92    0.0   \n",
       "4          18.31  24.13   60.38     7.78   7.72     0.00     0.00    0.0   \n",
       "...          ...    ...     ...      ...    ...      ...      ...    ...   \n",
       "29995       0.00   5.80    0.00    51.30   0.00     0.00     0.00    0.0   \n",
       "29996       0.00   0.00    0.00     0.00   0.00   100.00     0.00    0.0   \n",
       "29997       0.00   9.25    0.00    77.48  13.27     0.00     0.00    0.0   \n",
       "29998       0.00   0.00    0.00   100.00   0.00     0.00     0.00    0.0   \n",
       "29999       0.00   0.00    0.00     0.00   0.00     0.00     0.00  100.0   \n",
       "\n",
       "       Pets%  \n",
       "0        0.0  \n",
       "1        0.0  \n",
       "2        0.0  \n",
       "3        0.0  \n",
       "4        0.0  \n",
       "...      ...  \n",
       "29995   42.9  \n",
       "29996    0.0  \n",
       "29997    0.0  \n",
       "29998    0.0  \n",
       "29999    0.0  \n",
       "\n",
       "[30000 rows x 9 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "# 数据预处理\n",
    "df = pd.read_csv(\"../data/order.csv\")\n",
    "df.drop(['customer','order','total_items','weekday','hour'],axis=1,inplace=True)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>discount%</th>\n",
       "      <th>Food%</th>\n",
       "      <th>Fresh%</th>\n",
       "      <th>Drinks%</th>\n",
       "      <th>Home%</th>\n",
       "      <th>Beauty%</th>\n",
       "      <th>Health%</th>\n",
       "      <th>Baby%</th>\n",
       "      <th>Pets%</th>\n",
       "      <th>分类标签</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>23.03</td>\n",
       "      <td>9.46</td>\n",
       "      <td>87.06</td>\n",
       "      <td>3.48</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.22</td>\n",
       "      <td>15.87</td>\n",
       "      <td>75.80</td>\n",
       "      <td>6.22</td>\n",
       "      <td>2.12</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.08</td>\n",
       "      <td>16.88</td>\n",
       "      <td>56.75</td>\n",
       "      <td>3.37</td>\n",
       "      <td>16.48</td>\n",
       "      <td>6.53</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.51</td>\n",
       "      <td>28.81</td>\n",
       "      <td>35.99</td>\n",
       "      <td>11.78</td>\n",
       "      <td>4.62</td>\n",
       "      <td>2.87</td>\n",
       "      <td>15.92</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>18.31</td>\n",
       "      <td>24.13</td>\n",
       "      <td>60.38</td>\n",
       "      <td>7.78</td>\n",
       "      <td>7.72</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29995</th>\n",
       "      <td>0.00</td>\n",
       "      <td>5.80</td>\n",
       "      <td>0.00</td>\n",
       "      <td>51.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>42.9</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29996</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29997</th>\n",
       "      <td>0.00</td>\n",
       "      <td>9.25</td>\n",
       "      <td>0.00</td>\n",
       "      <td>77.48</td>\n",
       "      <td>13.27</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29998</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29999</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30000 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       discount%  Food%  Fresh%  Drinks%  Home%  Beauty%  Health%  Baby%  \\\n",
       "0          23.03   9.46   87.06     3.48   0.00     0.00     0.00    0.0   \n",
       "1           1.22  15.87   75.80     6.22   2.12     0.00     0.00    0.0   \n",
       "2          18.08  16.88   56.75     3.37  16.48     6.53     0.00    0.0   \n",
       "3          16.51  28.81   35.99    11.78   4.62     2.87    15.92    0.0   \n",
       "4          18.31  24.13   60.38     7.78   7.72     0.00     0.00    0.0   \n",
       "...          ...    ...     ...      ...    ...      ...      ...    ...   \n",
       "29995       0.00   5.80    0.00    51.30   0.00     0.00     0.00    0.0   \n",
       "29996       0.00   0.00    0.00     0.00   0.00   100.00     0.00    0.0   \n",
       "29997       0.00   9.25    0.00    77.48  13.27     0.00     0.00    0.0   \n",
       "29998       0.00   0.00    0.00   100.00   0.00     0.00     0.00    0.0   \n",
       "29999       0.00   0.00    0.00     0.00   0.00     0.00     0.00  100.0   \n",
       "\n",
       "       Pets%  分类标签  \n",
       "0        0.0     2  \n",
       "1        0.0     2  \n",
       "2        0.0     2  \n",
       "3        0.0     2  \n",
       "4        0.0     2  \n",
       "...      ...   ...  \n",
       "29995   42.9     0  \n",
       "29996    0.0     0  \n",
       "29997    0.0     0  \n",
       "29998    0.0     0  \n",
       "29999    0.0     1  \n",
       "\n",
       "[30000 rows x 10 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型训练\n",
    "model = AgglomerativeClustering(n_clusters=4, affinity='euclidean',linkage='ward')\n",
    "# 输入数据并训练模型并返回聚类标签\n",
    "prdictede_label = model.fit_predict(df)\n",
    "# 将标签添加到表格对象中\n",
    "# pd.read_csv(\"../tmp/data_dispose.csv\")\n",
    "df['分类标签'] = prdictede_label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "分类标签\n",
       "0    13792\n",
       "1     5202\n",
       "2     9265\n",
       "3     1741\n",
       "Name: 分类标签, dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(by='分类标签')['分类标签'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 算出每一类的平均购买情况\n",
    "label_0 = df.loc[df['分类标签'] == 0,:]\\\n",
    "    [['discount%','Food%','Fresh%','Drinks%','Home%','Beauty%','Health%','Baby%','Pets%']].mean().round(2)\n",
    "label_1 = df.loc[df['分类标签'] == 1,:]\\\n",
    "    [['discount%','Food%','Fresh%','Drinks%','Home%','Beauty%','Health%','Baby%','Pets%']].mean().round(2)\n",
    "label_2 = df.loc[df['分类标签'] == 2,:]\\\n",
    "    [['discount%','Food%','Fresh%','Drinks%','Home%','Beauty%','Health%','Baby%','Pets%']].mean().round(2)\n",
    "label_3 = df.loc[df['分类标签'] == 3,:]\\\n",
    "    [['discount%','Food%','Fresh%','Drinks%','Home%','Beauty%','Health%','Baby%','Pets%']].mean().round(2)\n",
    "\n",
    "# 画饼图进行观察\n",
    "pie_label = ['折扣','食物(非生鲜)','生鲜类食物','饮料','家居用品','美妆类产品','保健类产品','母婴类产品','宠物用品']\n",
    "# 数据准备\n",
    "list_0 = [ [i,j] for i,j in zip(pie_label,label_0.values.tolist())]\n",
    "list_1 = [ [i,j] for i,j in zip(pie_label,label_1.values.tolist())]\n",
    "list_2 = [ [i,j] for i,j in zip(pie_label,label_2.values.tolist())]\n",
    "list_3 = [ [i,j] for i,j in zip(pie_label,label_3.values.tolist())]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"093e9312d2014188a3ee747ea33125c3\" style=\"width:900px; height:900px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_093e9312d2014188a3ee747ea33125c3 = echarts.init(\n",
       "                    document.getElementById('093e9312d2014188a3ee747ea33125c3'), 'white', {renderer: 'canvas'});\n",
       "                var option_093e9312d2014188a3ee747ea33125c3 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"name\": \"\\u6807\\u7b7e0\\u7684\\u4eba\\u7fa4\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u6298\\u6263\",\n",
       "                    \"value\": 7.98\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u98df\\u7269(\\u975e\\u751f\\u9c9c)\",\n",
       "                    \"value\": 21.66\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u751f\\u9c9c\\u7c7b\\u98df\\u7269\",\n",
       "                    \"value\": 7.79\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u996e\\u6599\",\n",
       "                    \"value\": 35.59\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5bb6\\u5c45\\u7528\\u54c1\",\n",
       "                    \"value\": 21.04\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7f8e\\u5986\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 9.02\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4fdd\\u5065\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 1.5\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6bcd\\u5a74\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 1.4\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5ba0\\u7269\\u7528\\u54c1\",\n",
       "                    \"value\": 1.79\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                50,\n",
       "                80\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"20%\",\n",
       "                \"20%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"outside\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}:\\n{d}%\"\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"name\": \"\\u6807\\u7b7e1\\u7684\\u4eba\\u7fa4\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u6298\\u6263\",\n",
       "                    \"value\": 7.83\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u98df\\u7269(\\u975e\\u751f\\u9c9c)\",\n",
       "                    \"value\": 9.48\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u751f\\u9c9c\\u7c7b\\u98df\\u7269\",\n",
       "                    \"value\": 6.33\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u996e\\u6599\",\n",
       "                    \"value\": 13.25\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5bb6\\u5c45\\u7528\\u54c1\",\n",
       "                    \"value\": 8.47\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7f8e\\u5986\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 3.14\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4fdd\\u5065\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 0.88\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6bcd\\u5a74\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 57.88\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5ba0\\u7269\\u7528\\u54c1\",\n",
       "                    \"value\": 0.34\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                50,\n",
       "                80\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"60%\",\n",
       "                \"20%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"outside\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}:\\n{d}%\"\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"name\": \"\\u6807\\u7b7e2\\u7684\\u4eba\\u7fa4\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u6298\\u6263\",\n",
       "                    \"value\": 5.49\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u98df\\u7269(\\u975e\\u751f\\u9c9c)\",\n",
       "                    \"value\": 23.06\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u751f\\u9c9c\\u7c7b\\u98df\\u7269\",\n",
       "                    \"value\": 49.61\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u996e\\u6599\",\n",
       "                    \"value\": 13.66\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5bb6\\u5c45\\u7528\\u54c1\",\n",
       "                    \"value\": 7.31\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7f8e\\u5986\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 3.67\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4fdd\\u5065\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 0.9\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6bcd\\u5a74\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 1.28\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5ba0\\u7269\\u7528\\u54c1\",\n",
       "                    \"value\": 0.44\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                50,\n",
       "                80\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"20%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"outside\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}:\\n{d}%\"\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"name\": \"\\u6807\\u7b7e3\\u7684\\u4eba\\u7fa4\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u6298\\u6263\",\n",
       "                    \"value\": 29.89\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u98df\\u7269(\\u975e\\u751f\\u9c9c)\",\n",
       "                    \"value\": 83.33\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u751f\\u9c9c\\u7c7b\\u98df\\u7269\",\n",
       "                    \"value\": 3.88\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u996e\\u6599\",\n",
       "                    \"value\": 8.14\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5bb6\\u5c45\\u7528\\u54c1\",\n",
       "                    \"value\": 2.48\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7f8e\\u5986\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 1.12\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4fdd\\u5065\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 0.22\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6bcd\\u5a74\\u7c7b\\u4ea7\\u54c1\",\n",
       "                    \"value\": 0.36\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5ba0\\u7269\\u7528\\u54c1\",\n",
       "                    \"value\": 0.4\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                50,\n",
       "                80\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"60%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"outside\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}:\\n{d}%\"\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u6298\\u6263\",\n",
       "                \"\\u98df\\u7269(\\u975e\\u751f\\u9c9c)\",\n",
       "                \"\\u751f\\u9c9c\\u7c7b\\u98df\\u7269\",\n",
       "                \"\\u996e\\u6599\",\n",
       "                \"\\u5bb6\\u5c45\\u7528\\u54c1\",\n",
       "                \"\\u7f8e\\u5986\\u7c7b\\u4ea7\\u54c1\",\n",
       "                \"\\u4fdd\\u5065\\u7c7b\\u4ea7\\u54c1\",\n",
       "                \"\\u6bcd\\u5a74\\u7c7b\\u4ea7\\u54c1\",\n",
       "                \"\\u5ba0\\u7269\\u7528\\u54c1\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"type\": \"scroll\",\n",
       "            \"show\": true,\n",
       "            \"left\": \"80%\",\n",
       "            \"top\": \"20%\",\n",
       "            \"orient\": \"vertical\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"4\\u7c7b\\u4eba\\u7fa4\\u7684\\u5e73\\u5747\\u8d2d\\u4e70\\u60c5\\u51b5\\uff1a\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_093e9312d2014188a3ee747ea33125c3.setOption(option_093e9312d2014188a3ee747ea33125c3);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x15243cc9580>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Pie\n",
    "from pyecharts.commons.utils import JsCode\n",
    "\n",
    "# 用pyecahrt画图\n",
    "\n",
    "# 写一个函数来配置标签项\n",
    "def new_label_opts():\n",
    "    return opts.LabelOpts(\n",
    "            position=\"outside\",\n",
    "            formatter=\"{b}:\\n{d}%\",\n",
    "        )\n",
    "\n",
    "c = (\n",
    "    Pie(init_opts=opts.InitOpts(width='900px',height='900px'))\n",
    "    .add(\n",
    "        \"标签0的人群\",\n",
    "        list_0,\n",
    "        center=[\"20%\", \"20%\"],\n",
    "        radius=[50, 80],\n",
    "        label_opts=new_label_opts()\n",
    "    )\n",
    "    .add(\n",
    "        \"标签1的人群\",\n",
    "        list_1,\n",
    "        center=[\"60%\", \"20%\"],\n",
    "        radius=[50, 80],\n",
    "        label_opts=new_label_opts()\n",
    "    )\n",
    "    .add(\n",
    "        \"标签2的人群\",\n",
    "        list_2,\n",
    "        center=[\"20%\", \"50%\"],\n",
    "        radius=[50, 80],\n",
    "        label_opts=new_label_opts()\n",
    "    )\n",
    "    .add(\n",
    "        \"标签3的人群\",\n",
    "        list_3,\n",
    "        center=[\"60%\", \"50%\"],\n",
    "        radius=[50, 80],\n",
    "        label_opts=new_label_opts()\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"4类人群的平均购买情况：\"),\n",
    "        legend_opts=opts.LegendOpts(\n",
    "            type_=\"scroll\", pos_top=\"20%\", pos_left=\"80%\", orient=\"vertical\"\n",
    "        ),\n",
    "    )\n",
    ")\n",
    "c.render(\"../tmp/4类人群的平均购买情况(分层聚类).html\")\n",
    "c.render_notebook()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "79c2bc36f36ebca92fe5f3c73355a3866f3a34a4fd0b1d43087d5313dd5fd5ff"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
